﻿using ImperialWebSite.Presenters.Citizen.Classes;
using ImperialWebSite.Presenters.Citizen.Interfaces;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ImperialWebSite.Views.Main.Citizen
{
    public partial class QueryCitizens : Page, IQueryCitizens
    {
        private QueryCitizensPresenter _presenter;
        private bool _successfulTransaction;

        public object CitizensDataSource
        {
            set { gvCitizens.DataSource = value; }
        }


        public object RolesDataSource
        {
            set { Roles.DataSource = value; }
        }

        public string RolesDataTextField
        {
            set { Roles.DataTextField = value; }
        }

        public string RolesDataValueField
        {
            set { Roles.DataValueField = value; }
        }


        public object SpeciesDataSource
        {
            set { Species.DataSource = value; }
        }

        public string SpeciesDataTextField
        {
            set { Species.DataTextField = value; }
        }

        public string SpeciesDataValueField
        {
            set { Species.DataValueField = value; }
        }


        public object StatusesDataSource
        {
            set { Statuses.DataSource = value; }
        }

        public string StatusesDataTextField
        {
            set { Statuses.DataTextField = value; }
        }

        public string StatusesDataValueField
        {
            set { Statuses.DataValueField = value; }
        }


        public string RealnameText
        {
            get { return Realname.Text; }
        }

        public string UsernameText
        {
            get { return Username.Text; }
        }


        public string RoleValue
        {
            get { return Roles.SelectedItem.Value; }
        }

        public string RoleText
        {
            get { return Roles.SelectedItem.Text; }
        }

        
        public string SpecieValue
        {
            get { return Species.SelectedItem.Value; }
        }

        public string SpecieText
        {
            get { return Species.SelectedItem.Text; }
        }

        
        public string StatusValue
        {
            get { return Statuses.SelectedItem.Value; }
        }

        public string StatusText
        {
            get { return Statuses.SelectedItem.Text; }
        }


        public object SessionCurrentCitizens
        {
            get { return Session["currentCitizens"]; }
            set { Session["currentCitizens"] = value; }
        }

        public object SessionCurrentCitizen
        {
            get { return Session["currentCitizen"]; }
            set { Session["currentCitizen"] = value; }
        }


        public string TransactionMessage
        {
            get { return FailureText.Text; }
            set { FailureText.Text = value; }
        }

        public bool SuccessfulTransaction
        {
            get { return _successfulTransaction; }
            set { _successfulTransaction = value; }
        }


        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            _presenter = new QueryCitizensPresenter(this);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                InitMessages();
                InitComponents();
            }
        }

        private void InitMessages()
        {
            SuccessfulTransaction = false;
            TransactionMessage = string.Empty;
        }

        private void InitComponents()
        {
            SessionCurrentCitizen = null;
            SessionCurrentCitizens = null;
            LoadDropdownLists();
            LoadCurrentCitizens();
            Username.Text = string.Empty;
            Realname.Text = string.Empty;
            //btnUpdate.Enabled = SessionCurrentCitizen != null;
        }

        private void LoadCurrentCitizens()
        {
            _presenter.LoadCurrentCitizens();
            gvCitizens.DataBind();
        }

        private void LoadDropdownLists()
        {
            try
            {
                _presenter.LoadRolesDropdownLists();
                Roles.DataBind();
                _presenter.LoadSpeciesDropdownLists();
                Species.DataBind();
                _presenter.LoadStatusesDropdownLists();
                Statuses.DataBind();
            }
            catch (Exception e)
            {
                TransactionMessage = "The empire is under attack: " + e.Message;
            }
        }


        protected void GvCitizens_OnPageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvCitizens.DataSource = SessionCurrentCitizens;
            gvCitizens.PageIndex = e.NewPageIndex;
            gvCitizens.DataBind();
            SessionCurrentCitizen = null;
            Username.Text = string.Empty;
            Realname.Text = string.Empty;

        }

        protected void GvCitizens_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            GridView gv = sender as GridView;
            gv.SelectedIndex = int.Parse(e.CommandArgument.ToString());
            Session["currentCitizen"] = gv.SelectedDataKey["idUser"].ToString();
            TransactionMessage = string.Empty;
            Username.Text = gv.SelectedRow.Cells[0].Text;
            Realname.Text = gv.SelectedRow.Cells[1].Text;
            Roles.SelectedValue = gv.SelectedDataKey["fkRole"].ToString();
            Species.SelectedValue = gv.SelectedDataKey["fkSpecie"].ToString();
            Statuses.SelectedValue = gv.SelectedDataKey["idStatus"].ToString();
            //btnUpdate.Enabled = SessionCurrentCitizen != null;
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            if (UsernameText != string.Empty && RealnameText != string.Empty && SessionCurrentCitizen != null)
            {
                _presenter.UpdateCitizen();
                if (SuccessfulTransaction)
                    InitComponents();
            }
            else if (SessionCurrentCitizen == null)
                TransactionMessage = "Please select a Citizen";
        }
    }
}